package l.l.w.practice.EightQueens;

import java.awt.*;
import java.util.Arrays;

/**
 * Created by llw on 2017/9/2.
 */
public class EightQueens {

    public int getAllQueens(int num){
        int count=0;
        int[] location = new int[num];
        getQueens(count,location,num,0);
        return count;
    }

    private void getQueens(int count, int[] location, int num,int i) {
        if(i >= num){
            System.out.println(Arrays.toString(location));
            count++;
        }
        for(int j=0;j<num;++j){
            location[i]=j;
            if(check(location,num,i)){
                getQueens(count,location,num,i+1);
            }
        }
    }

    private boolean check(int[] location, int num, int i) {
        for(int j=0;j<i;++j){
            if(Math.abs(location[i]-location[j])==Math.abs(i-j) || location[i]==location[j])
                return false;
        }
        return true;
    }

    public static void main(String[] args) {
        int num = 8;
        new EightQueens().getAllQueens(8);
    }
}
